package com.feeyin.radar.msg.service.impl;

import com.feeyin.radar.commons.DateUtils;
import com.feeyin.radar.commons.Page;
import com.feeyin.radar.commons.StringUtils;
import com.feeyin.radar.msg.dao.InternalMailboxDAO;
import com.feeyin.radar.msg.model.InternalMailbox;
import com.feeyin.radar.msg.service.InternalMailboxService;
import com.feeyin.radar.sys.dao.UserDAO;
import com.feeyin.radar.sys.model.Users;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class InternalMailboxServiceImpl
  implements InternalMailboxService
{
  private InternalMailboxDAO internalMailboxDAO;
  private UserDAO userDAO;

  public InternalMailboxDAO getInternalMailboxDAO()
  {
    return this.internalMailboxDAO;
  }

  public void setInternalMailboxDAO(InternalMailboxDAO internalMailboxDAO) {
    this.internalMailboxDAO = internalMailboxDAO;
  }

  public UserDAO getUserDAO() {
    return this.userDAO;
  }

  public void setUserDAO(UserDAO userDAO) {
    this.userDAO = userDAO;
  }

  public List getPageInternalMailboxs(Integer userId, Page page, int flag, String subject) {
    if (page == null) return null;
    if (userId == null) return null;
    InternalMailbox internalMailbox = new InternalMailbox();
    if (!StringUtils.isEmpty(subject)) {
      internalMailbox.setSubject(subject);
    }
    if (flag == 0) {
      internalMailbox.setRecipientsId(userId);
      internalMailbox.setIsDeleteInbox("0");
    } else if (flag == 1) {
      internalMailbox.setAddresserId(userId);
      internalMailbox.setIsDeleteOutbox("0");
    } else if (flag == 2) {
      internalMailbox.setIsWasteMail("1");
      internalMailbox.setUserId(userId);
    } else {
      return null;
    }

    List internalMailboxs = new ArrayList();
    internalMailboxs = this.internalMailboxDAO.getPageInternalMailboxsByDynamic(internalMailbox, page);

    if (internalMailboxs == null) return null;
    for (int i = 0; i < internalMailboxs.size(); i++) {
      internalMailbox = new InternalMailbox();
      internalMailbox = (InternalMailbox)internalMailboxs.get(i);
      if (internalMailbox.getAddresserId() != null) {
        Users user = new Users();
        user = this.userDAO.getUser(internalMailbox.getAddresserId());
        if (user != null) {
          internalMailbox.setAddresser(user.getUserName());
        }
      }
      if (internalMailbox.getRecipientsId() != null) {
        Users user = new Users();
        user = this.userDAO.getUser(internalMailbox.getRecipientsId());
        if (user != null) {
          internalMailbox.setRecipients(user.getUserName());
        }
      }
    }
    return internalMailboxs;
  }

  public Integer getPageInternalMailboxsCount(Integer userId, int flag, String subject) {
    if (userId == null) return Integer.valueOf(0);
    InternalMailbox internalMailbox = new InternalMailbox();
    if (!StringUtils.isEmpty(subject)) {
      internalMailbox.setSubject(subject);
    }
    if (flag == 0) {
      internalMailbox.setRecipientsId(userId);
      internalMailbox.setIsDeleteInbox("0");
    } else if (flag == 1) {
      internalMailbox.setAddresserId(userId);
      internalMailbox.setIsDeleteOutbox("0");
    } else if (flag == 2) {
      internalMailbox.setIsWasteMail("1");
      internalMailbox.setUserId(userId);
    } else {
      return Integer.valueOf(0);
    }
    return this.internalMailboxDAO.getPageInternalMailboxsCountByDynamic(internalMailbox);
  }

  public InternalMailbox getInternalMailById(Integer id) {
    if (id == null) return null;
    InternalMailbox internalMailbox = new InternalMailbox();
    internalMailbox = this.internalMailboxDAO.getInternalMailById(id);
    if (internalMailbox == null) return null;

    if (internalMailbox.getAddresserId() != null) {
      Users user = new Users();
      user = this.userDAO.getUser(internalMailbox.getAddresserId());
      if (user != null) {
        internalMailbox.setAddresser(user.getUserName());
      }
    }
    if (internalMailbox.getRecipientsId() != null) {
      Users user = new Users();
      user = this.userDAO.getUser(internalMailbox.getRecipientsId());
      if (user != null) {
        internalMailbox.setRecipients(user.getUserName());
      }
    }
    return internalMailbox;
  }

  public Integer getNoReadInternalMailCount(Integer userId) {
    if (userId == null) return Integer.valueOf(0);
    Integer count = Integer.valueOf(0);
    InternalMailbox internalMailbox = new InternalMailbox();
    internalMailbox.setRecipientsId(userId);
    internalMailbox.setIsDeleteInbox("0");
    internalMailbox.setIsRead("0");
    count = this.internalMailboxDAO.getPageInternalMailboxsCountByDynamic(internalMailbox);
    if (count == null) return Integer.valueOf(0);
    return count;
  }

  public boolean insertInternalMail(InternalMailbox internalMailbox) {
    if (internalMailbox == null) return false;
    String[] recipientsIds = internalMailbox.getRecipientsIds().split(",");
    if ((recipientsIds == null) || (recipientsIds.length <= 0)) return false;
    String code = DateUtils.getDateByFormat("yyyyMMddHHmmss");
    internalMailbox.setCode(code);
    internalMailbox.setCreateDate(new Date());
    internalMailbox.setIsDeleteOutbox("0");
    internalMailbox.setIsDeleteInbox("0");
    internalMailbox.setIsRead("0");

    for (int i = 0; i < recipientsIds.length; i++) {
      if (!StringUtils.isEmpty(recipientsIds[i])) {
        internalMailbox.setRecipientsId(Integer.valueOf(Integer.parseInt(recipientsIds[i])));
        this.internalMailboxDAO.insertInternalMail(internalMailbox);
      }

    }

    return true;
  }

  public boolean updateInternalMailForReaded(Integer id) {
    if (id == null) return false;
    InternalMailbox internalMailbox = new InternalMailbox();
    internalMailbox.setId(id);
    internalMailbox.setIsRead("1");
    return this.internalMailboxDAO.updateInternalMailByDynamic(internalMailbox);
  }

  public boolean moveInternalMailToWastebox(String[] ids, Integer userId, int sign)
  {
    if ((ids == null) || (ids.length <= 0)) return false;
    if (userId == null) return false;

    InternalMailbox internalMail = new InternalMailbox();
    internalMail.setIsDeleteInbox("2");
    internalMail.setIsDeleteOutbox("2");
    this.internalMailboxDAO.deleteInternalMailByDynamic(internalMail);

    for (int i = 0; i < ids.length; i++) {
      if (!StringUtils.isEmpty(ids[i])) {
        if (sign == 1) {
          InternalMailbox internalMailbox = new InternalMailbox();
          internalMailbox.setIsDeleteInbox("1");
          internalMailbox.setId(Integer.valueOf(Integer.parseInt(ids[i])));
          this.internalMailboxDAO.updateInternalMailByDynamic(internalMailbox);
        } else if (sign == 2) {
          InternalMailbox internalMailbox = new InternalMailbox();
          internalMailbox.setIsDeleteOutbox("1");
          internalMailbox.setId(Integer.valueOf(Integer.parseInt(ids[i])));
          this.internalMailboxDAO.updateInternalMailByDynamic(internalMailbox);
        }
      }
    }

    return true;
  }

  public boolean deleteInternalMail(String[] ids, Integer userId, int sign) {
    if ((ids == null) || (ids.length <= 0)) return false;
    if (userId == null) return false;

    InternalMailbox internalMail = new InternalMailbox();
    internalMail.setIsDeleteInbox("2");
    internalMail.setIsDeleteOutbox("2");
    this.internalMailboxDAO.deleteInternalMailByDynamic(internalMail);

    for (int i = 0; i < ids.length; i++) {
      if (!StringUtils.isEmpty(ids[i])) {
        if (sign == 1) {
          InternalMailbox internalMailbox = new InternalMailbox();
          internalMailbox.setIsDeleteInbox("2");
          internalMailbox.setId(Integer.valueOf(Integer.parseInt(ids[i])));
          this.internalMailboxDAO.updateInternalMailByDynamic(internalMailbox);
        } else if (sign == 2) {
          InternalMailbox internalMailbox = new InternalMailbox();
          internalMailbox.setIsDeleteOutbox("2");
          internalMailbox.setId(Integer.valueOf(Integer.parseInt(ids[i])));
          this.internalMailboxDAO.updateInternalMailByDynamic(internalMailbox);
        } else if (sign == 3) {
          InternalMailbox internalMailbox = new InternalMailbox();
          internalMailbox = this.internalMailboxDAO.getInternalMailById(Integer.valueOf(Integer.parseInt(ids[i])));
          if (internalMailbox != null) {
            if ((!"0".equals(internalMailbox.getIsDeleteInbox())) && 
              (!"0".equals(internalMailbox.getIsDeleteOutbox()))) {
              this.internalMailboxDAO.deleteInternalMailById(Integer.valueOf(Integer.parseInt(ids[i])));
            } else {
              if ((internalMailbox.getRecipientsId().compareTo(userId) == 0) && 
                ("1".equals(internalMailbox.getIsDeleteInbox())) && 
                ("0".equals(internalMailbox.getIsDeleteOutbox()))) {
                internalMailbox = new InternalMailbox();
                internalMailbox.setIsDeleteInbox("2");
                internalMailbox.setId(Integer.valueOf(Integer.parseInt(ids[i])));
                this.internalMailboxDAO.updateInternalMailByDynamic(internalMailbox);
              }
              if ((internalMailbox.getAddresserId().compareTo(userId) != 0) || 
                (!"1".equals(internalMailbox.getIsDeleteOutbox())) || 
                (!"0".equals(internalMailbox.getIsDeleteInbox()))) continue;
              internalMailbox = new InternalMailbox();
              internalMailbox.setIsDeleteOutbox("2");
              internalMailbox.setId(Integer.valueOf(Integer.parseInt(ids[i])));
              this.internalMailboxDAO.updateInternalMailByDynamic(internalMailbox);
            }
          }
        }
      }

    }

    return true;
  }
}